//package com.cssy.yd.basic.web;
//
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//
//import com.alibaba.fastjson.JSONArray;
//import com.cssy.base.web.BaseController;
//import com.cssy.base.web.BodyMsg;
//import com.cssy.yd.basic.entity.BasicAreaDO;
//import com.cssy.yd.basic.entity.BasicCompanyDO;
//import com.cssy.yd.basic.entity.BasicCompanytypeDO;
//import com.cssy.yd.basic.entity.BasicFundamentalUnitDO;
//import com.cssy.yd.basic.entity.BasicMaterialsDO;
//import com.cssy.yd.basic.entity.BasicMaterialstypeDO;
//import com.cssy.yd.basic.entity.BasicMeasurementUnitDO;
//import com.cssy.yd.basic.entity.BasicStorageinfoDO;
//import com.cssy.yd.basic.service.IBasicAreaService;
//import com.cssy.yd.basic.service.IBasicCompanyService;
//import com.cssy.yd.basic.service.IBasicFundamentalUnitService;
//import com.cssy.yd.basic.service.IBasicMaterialsService;
//import com.cssy.yd.basic.service.IBasicMaterialstypeService;
//import com.cssy.yd.basic.service.IBasicMeasurementUnitService;
//import com.cssy.yd.basic.service.IBasicStorageService;
//import com.cssy.yd.basic.util.MaterailsUtil;
//import com.cssy.yd.logic.entity.LogicWhCheckDetailsDO;
//import com.cssy.yd.logic.entity.LogicWhCheckMissionItemDO;
//import com.cssy.yd.logic.util.SqlUtil;
//import com.plat.exception.SyBaseException;
//import com.plat.helper.JqGridHandler;
//import com.plat.helper.SearchCondition;
//import com.plat.helper.SearchConditions;
//import com.plat.helper.SearchResult;
//import com.plat.util.ExcelUtil;
//import com.plat.util.GlobalResLoader;
//import com.plat.util.OperationFlag;
//import com.plat.util.SessionUtil;
//import com.plat.util.json.OutJsonGrid;
//import com.yuendong.sy.po.code.SyCode;
//import com.yuendong.sy.service.code.SyCodeType;
//import com.yuendong.sy.service.code.impl.SyCodeService;
//@Controller
//@RequestMapping("/yd/basic/materials")
//public class BasicMaterialsController extends BaseController<BasicMaterialsDO>{
//	private static final String itemtype="材料分类";
//	private static final String chenpin="成品";
//	private static final String EIDT = "/yd/basic/materials/MaterialsOperation";//添加、修改、显示页面
//	private static final String BROWSE = "/yd/basic/materialsBrowse/MaterialsBrowseOperation";
//	
//	@Resource(name="basicmatertypeService")
//	private IBasicMaterialstypeService basicmatertypeService;
//	
//	@Resource(name="basicMaterialsService")
//	private IBasicMaterialsService basicMaterialsService;
//	
//	@Resource(name="syCodeService")
//	private SyCodeService syCodeService;
//	
//	@Resource(name="basicAreaService")
//	private IBasicAreaService basicAreaService;
//	
//	@Resource(name = "basicwhService")
//	private IBasicStorageService basicwhService;
//	
//	@Resource(name = "basicFundamentalUnitService")
//	private IBasicFundamentalUnitService basicFundamentalUnitService;
//	
//	@Resource(name = "basicCompanyService")
//	private IBasicCompanyService basicCompanyService;
//	
//	@Resource(name="basicMeasurementUnitService")
//	private IBasicMeasurementUnitService basicMeasurementUnitService;
//	
//	public String manager(HttpServletRequest request, Model model)
//			throws Exception {
//		return null;
//	}
//	
//	
//	@Override
//	public void list(HttpServletRequest request, HttpServletResponse response)
//			throws Exception {
//		JqGridHandler hander=new JqGridHandler(request);
//		SearchConditions scs = new SearchConditions();
//		scs.setRemovedProperty("removed");//判断是否有删除标识
//		String ID = request.getParameter("ID");
//		String pid = request.getParameter("pid");
//		String idlist = null;
//		hander.getWheres(scs);	
//		scs.setLimitClause(null);
//		if(ID!=null&&!"".equals(ID)){
//			List<BasicMaterialstypeDO> list = basicmatertypeService.findByParentId(ID);
//			if(list.size()>0){
//				idlist = idList(list,ID);
//				scs.addCondition("itmetypeid", "\'"+idlist+"\'", SearchCondition.OP_IN);
//			}else{
//				scs.addCondition("itmetypeid", ID, SearchCondition.OP_EQUAL);
//			}
//			
//		}else if(pid!=null&&!"".equals(pid)){
//			scs.addCondition("itmetypeid", pid,SearchCondition.OP_EQUAL);
//		}
//		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
//			 scs.setOrderBy("code desc");
//		scs.addCondition("typeName", "原材料物料",SearchCondition.OP_EQUAL);
//		SearchResult result=	basicMaterialsService.findListByExample(scs);
//		List<BasicMaterialsDO> m = result.getResults();
//		OutJsonGrid out=new OutJsonGrid(result);
//		out.printJSon(response);
//		
//	}
//	
//	private String  idList(List<BasicMaterialstypeDO> list ,String parentid) throws SyBaseException {
//		List<BasicMaterialstypeDO> list1 =null;
//		List<String> ids = new ArrayList<String>();
//		StringBuffer sf = new StringBuffer();
//		ids.add(parentid);
//		for(int i=0;i<list.size();i++){
//			if(itemtype.equals(list.get(i).getParentname())&&chenpin.equals(list.get(i).getName())){
//				continue;
//			}
//			list1 =basicmatertypeService.findByParentId(list.get(i).getId());
//			if(list1.size()>0){
//				ids.add(idList(list1,list.get(i).getId()));
//			}else{
//				ids.add(list.get(i).getId());
//			}
//		}	
//		for(int i=0;i<ids.size();i++){
//			sf.append(ids.get(i));
//			if(i==(ids.size()-1)){
//			}else{
//				sf.append("\',\'");
//			}
//		}	
//		return sf.toString();
//	}
//	
//	@Override
//	public String operationForm(HttpServletRequest request,@PathVariable String id,
//			@PathVariable String flag, Model model) throws Exception {
//		String browse = request.getParameter("browse");
//		SearchConditions scs = new SearchConditions();
//		scs.setOrderBy("name asc");
//		List<BasicStorageinfoDO> bStorageinfoDOs =basicwhService.findAllByExample(scs).getResults();
//		
//		SearchConditions scs1 = new SearchConditions();
//		scs1.addCondition("use_system", "MES",SearchCondition.OP_EQUAL);
//		List<BasicAreaDO>  areaDOs =  basicAreaService.findListByExample(scs1).getResults();
//		
//		SearchConditions scs2 = new SearchConditions();
//		scs2.addCondition("parentname", "MES供应商",SearchCondition.OP_EQUAL);
//		scs2.setOrderBy("code ASC");
//		List<BasicCompanyDO> companyDOs = basicCompanyService.findListByExample(scs2).getResults();
//
//		 SearchConditions scs3 = new SearchConditions();
//			if(scs3.getOrderBy()==null||"".equals(scs3.getOrderBy()))
//				scs3.setOrderBy("unitcode");	
//		 SearchResult result = basicFundamentalUnitService.findListByExample(scs3);
//		 List<BasicFundamentalUnitDO> unitDOs = result.getResults();
//		 
//		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
//			BasicMaterialstypeDO hub=	basicmatertypeService.findListById(id);
//			
//			List<BasicMaterialstypeDO> basicMaterialsDOs = new ArrayList<BasicMaterialstypeDO>();
//			basicMaterialsDOs = this.getParetName(hub.getId(), basicMaterialsDOs);
//			Collections.reverse(basicMaterialsDOs);
//			
//			if(basicMaterialsDOs.size() > 3){
//				model.addAttribute("wlTypeRemark", basicMaterialsDOs.get(1));
//				model.addAttribute("lhTypeRemark", basicMaterialsDOs.get(2));
//			}
//			
//			model.addAttribute("pname", hub.getName());
//			model.addAttribute("pid", hub.getId());
//			model.addAttribute("flag", "add");
//		}else{
//			BasicMaterialsDO bd =basicMaterialsService.findListById(id);
//			model.addAttribute("pname", bd.getItmetype());
//			model.addAttribute("pid", bd.getItmetypeid());
//			model.addAttribute("materials", bd);
//			model.addAttribute("flag", "edit");
//		}
//		model.addAttribute("unitDOs", unitDOs);
//		model.addAttribute("bStorageinfoDOs", bStorageinfoDOs);
//		model.addAttribute("areaDOs", areaDOs);
//		model.addAttribute("companyDOs", companyDOs);
//		if(browse!=null && browse.equals("true")){
//			model.addAttribute("flag", "browse");
//			return BROWSE;
//		}else{
//			return EIDT;
//		}
//	}
//	
//	
//	@Override
//	public Map<String, Object> save(HttpServletRequest request,
//			BasicMaterialsDO curAP, BindingResult result, Model model)
//	{
//		if(curAP.getHighestInventory() == null){
//			curAP.setHighestInventory(0.0);
//		}
//		if(curAP.getMinmunInventory() == null){
//			curAP.setMinmunInventory(0.0);
//		}
//		if(curAP.getSafetyStock() == null){
//			curAP.setSafetyStock(0.0);
//		}
//		BodyMsg bodyMap=BodyMsg.getInstance();
//		try {
//				if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
//					basicMaterialsService.update(curAP, SessionUtil.getUser(request));
//					bodyMap.isSuccess();
//					bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
//					bodyMap.configWebId(curAP.getId());
//				}else{//新增
//					curAP.setTypeName("原材料物料");
//					BasicMaterialsDO cdo=null;
//					if("".equals(curAP.getCode())&&curAP.getCode()==null){
//						curAP.setCode(syCodeService.getCode("IM","IM",SyCode.DATE_YYMM,5));
//					}
//					List<BasicMaterialsDO> bdlist=basicMaterialsService.findcode(curAP.getCode());
//					if(bdlist.size()>0){
//						bodyMap.isFail();
//						bodyMap.configWebInfor("物料编码已经存在");
//						return bodyMap.modelMap;
//					}
//					BasicMaterialsDO maDo = new BasicMaterialsDO();
//					maDo.setPlaceCode(curAP.getPlaceCode());
//					maDo.setMaterialTypeCode(curAP.getMaterialTypeCode());
//					maDo.setMaterialNumberCategoryCode(curAP.getMaterialNumberCategoryCode());
////					maDo.setSupplierCode(curAP.getSupplierCode());
//					String list = basicMaterialsService.findMax(maDo);
//					new MaterailsUtil().getCode(curAP,list);
//					String pname=request.getParameter("pname");
//					String pid=request.getParameter("pid");
//					curAP.setItmetype(pname);
//					curAP.setItmetypeid(pid);
//					try {
//						cdo=basicMaterialsService.save(curAP, SessionUtil.getUser(request));
//					} catch (Exception e) {
//						e.printStackTrace();
//						bodyMap.isFail();
//						bodyMap.configWebInfor("数据出现重复条目，请检查后重新输入");
//						return bodyMap.modelMap;
//					}
//					
//					bodyMap.isSuccess();
//					bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
//					if(cdo!=null){
//						bodyMap.configWebId(cdo.getId());
//					}
//					
//					BasicMeasurementUnitDO bUnitDO = new BasicMeasurementUnitDO();
//					bUnitDO.setMaterialsid(cdo.getId());
//					bUnitDO.setMaterialscode(cdo.getCode());
//					bUnitDO.setMaterialsname(cdo.getName());
//					List<BasicMeasurementUnitDO> listBUnitDO = basicMeasurementUnitService.findListByExample(bUnitDO);
//					if(listBUnitDO.size() > 0){
//						for (BasicMeasurementUnitDO basicMeasurementUnitDO : listBUnitDO) {
//							if(basicMeasurementUnitDO.getDefaultUnit()== 0){
//								basicMeasurementUnitDO.setDefaultUnit(1);
//								basicMeasurementUnitService.update(basicMeasurementUnitDO, SessionUtil.getUser(request));
//							}
//						}
//					}
//					bUnitDO.setUnitname(cdo.getUnit());
//					bUnitDO.setUnitcode(syCodeService.getCode("UNIT","UNIT",SyCode.DATE_YYMM,5));
//					bUnitDO.setDefaultUnit(0);
//					BasicMeasurementUnitDO bud=basicMeasurementUnitService.save(bUnitDO, SessionUtil.getUser(request));
//				}
//		} catch (Exception e) {			
//			e.printStackTrace();
//			bodyMap.isFail();
//			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
//		}
//		return bodyMap.modelMap;
//		
//	}
//		
//
//	@Override
//	public Map<String, Object> delete(HttpServletRequest request,
//			HttpServletResponse response, Model model) throws Exception {
//		BodyMsg bodyMap=BodyMsg.getInstance();
//		try{
//			String ids = request.getParameter("ids");
//			String[] fields=ids.split(",");
//			
//			for(String field : fields){
//				basicMaterialsService.delete(field, SessionUtil.getUser(request));
//			}
//			
//			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
//		}catch(Exception e){
//			e.printStackTrace();
//			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
//		}
//		return bodyMap.modelMap;
//	}
//	
//	public List<BasicMaterialstypeDO> getParetName(String parentId,List<BasicMaterialstypeDO> basicMaterialsDOs) throws SyBaseException {
//		if(parentId != null &&  parentId.length() > 0){
//			SearchConditions scs = new SearchConditions();
//			scs.addCondition("id", parentId,SearchCondition.OP_EQUAL);
//			List<BasicMaterialstypeDO> list =basicmatertypeService.findListByExample(scs).getResults();
//			basicMaterialsDOs.add(list.get(0));
//			getParetName(list.get(0).getParentid(), basicMaterialsDOs);
//		}
//		return basicMaterialsDOs;
//	}
//	
//	@RequestMapping("/exportExcel")
//	public void exportAll(HttpServletRequest request,
//			HttpServletResponse response) throws Exception {
//		List<BasicMaterialsDO> list = null;
//		String idlist = null;
//		JqGridHandler hander = new JqGridHandler(request);
//		SearchConditions scs = new SearchConditions();
//		scs.clearCondition();
//		hander.setFilters(new String(request.getParameter("filters").getBytes(
//				"ISO-8859-1"), "UTF-8")); // 转码
//		hander.getWheres(scs);
//		scs.setLimitClause(null);
//		scs.setRemovedProperty("removed");//判断是否有删除标识
//		String ID =request.getParameter("ID");
//	//	hander.getWheres(scs);	
//		if(ID!=null&&!"".equals(ID)){
//			List<BasicMaterialstypeDO> list1 = basicmatertypeService.findByParentId(ID);
//			if(list1.size()>0){
//				idlist = idList(list1,ID);
//				scs.addCondition("itmetypeid", "\'"+idlist+"\'", SearchCondition.OP_IN);
//			}else{
//				scs.addCondition("itmetypeid", ID, SearchCondition.OP_EQUAL);
//			}
//		}
//		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
//			 scs.setOrderBy("code desc");
//		scs.addCondition("typeName", "原材料物料",SearchCondition.OP_EQUAL);
//		SearchResult result=	basicMaterialsService.findListByExample(scs);
//		list = result.getResults();
//		ExcelUtil.toExcel(list, "物料料号:code┋物料名称:name┋物料规格:spec┋单位:unit┋描述:description┋财务分类:remark1┋备注:remark","原材料物料列表导出",response);
//	}
//}
